package service.impl;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.StoreDao;
import net.sf.json.JSONArray;
import service.StoreManager;
import util.Sendmail;
import vo.Commodity;
import vo.CommodityItem;
import vo.OrderForm;
import vo.Store;
@Service("sm")
public class StoreManagerImpl implements StoreManager {
	@Autowired(required=true)
	private StoreDao sd;
	@Override
	public Store login(String email, String passwd) {
		return sd.findStu(email,passwd);
	}
	

	
	@Override
	@Transactional
	public Store updatePasswd(String email, String password, String newpassword) {
		Store store =  sd.findStu(email,password);
		if (store != null) {
			if (sd.updatePasswd(store.getEmail(),newpassword) >0) {
				store.setPasswd(newpassword);
			}
		}
		store.setEmail(null);
		return store;
	}

	@Override
	@Transactional
	public int regist( String email,String passwd) {
		//s.setPsd(BaseUtil.JohnMd(s.getPsd(), "md5"));
		return sd.addStu(email, passwd);
	}

	@Override
	@Transactional
	public int apply(Store s) {
		
		return sd.post(s);
	}

	@Override
	public Store getInfoByEmail(String email, Store s) {
		s = sd.getInfoByEmail(email, s);
		System.out.println("this is service:" + s.toString());
		return s;
	}

	@Override
	@Transactional
	public boolean alterStatus(String email,int status) {
		
		
		return sd.updateStatus(email,status)>0? true:false;
		
	}

	@Override
	public List<Commodity> findFood(String email) {
		
		return sd.findFood(email);
	}

	@Override
	@Transactional
	public int addFood(Commodity commodity) {
		return sd.insertFood(commodity);
	}

	@Override
	@Transactional
	public int deleteFood(int id) {
		
		Commodity commodity = sd.findFoodById(id);
		
		return sd.deleteFood(commodity);
	}
	//鏌ヨ璁㈠崟
		@Override
		public List<OrderForm> findOrdersByStoreId(String storeId) {
			// TODO Auto-generated method stub
			return sd.findStoreOrderForms(storeId);
		}
	//鏌ヨ璁㈠崟鏉＄洰
		@Override
		public List<CommodityItem> findItemsByOrderId(String orderNumber) {
			// TODO Auto-generated method stub
			return sd.findItemsByOrderId(orderNumber);
		} 
		
		@Override
		@Transactional
		public boolean bindingMail(Store store,String newemail) {
			
			System.out.println("this is service:" + store.getEmail() + store.getPasswd()+ newemail);
			if(sd.findStu(store.getEmail(), store.getPasswd())!=null) {
				if (sendMail(store)) {
					System.out.println("this is sendmail");
					if(sd.updateMailAndTel(store,newemail)) {
						
						return true;
					}
					
				}
			}
		
			return false;
		}
		@Override
		public boolean sendMail(Store store) {
			boolean flag = false;
			try {
				Sendmail send = new Sendmail(store.getEmail(), "娉ㄥ唽鎴愬姛", "灏婃暚鐨�" + store.getSellerName() + "鎮ㄥ凡缁忔垚鍔熸敞鍐屼簡锛岃阿璋㈡偍鐨勬敮鎸�");
				//鐢ㄦ埛娉ㄥ唽鎴愬姛涔嬪悗灏变娇鐢ㄧ敤鎴锋敞鍐屾椂鐨勯偖绠辩粰鐢ㄦ埛鍙戦�佷竴灏丒mail
				//鍙戦�侀偖浠舵槸涓�浠堕潪甯歌�楁椂鐨勪簨鎯咃紝鍥犳杩欓噷寮�杈熶簡鍙︿竴涓嚎绋嬫潵涓撻棬鍙戦�侀偖浠�
				send.start();
				//绛夊緟鍙戦��
				Thread.sleep(2000);
				flag = true;
			} catch (Exception e) {
				flag = false;
				return flag;
			}
			if (flag) {
				
			}
			
			return flag;
		}

		@Override
		@Transactional
		public int deleteTouSuByOrderId(Integer orderId) {
			// TODO Auto-generated method stub
			return sd.deleteTouSuByOrderId(orderId);
		}


		@Override
		@Transactional
		public List<OrderForm> findOrders(String email) {
			
			return sd.findStoreOrderForms(email);
			
		}
		
		
	
}
